package com.hspedu.newData.LCR;

import com.hspedu.newData.dmsxl.ds.TreeNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-26 
 * @Version: 1.0
 */

// todo
public class LCR49求根节点到叶节点数字之和 {

    public int sumNumbers(TreeNode root) {

        int dfs = dfs(root, 0);
        return dfs;

    }

    public int dfs(TreeNode root,int prevSum){
        if (root==null) return 0;

        int sum = prevSum*10+root.val;
        if (root.left == null && root.right == null){
            return sum;
        }else {
            return dfs(root.left,sum) + dfs(root.right,sum);
        }
    }
}
